package com.xinke.module.system.controller.admin.user;

import cn.hutool.core.collection.CollUtil;
import com.xinke.component.common.enums.CommonStatusEnum;
import com.xinke.module.system.convert.post.PostConvert;
import com.xinke.module.system.convert.user.UserConvert;
import com.xinke.module.system.model.entity.user.AdminUserDO;
import com.xinke.module.system.model.entity.user.DeptDO;
import com.xinke.module.system.service.user.DeptService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;

import static com.xinke.component.common.util.collection.CollectionUtils.convertList;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.user.*;
import com.xinke.module.system.model.entity.user.PostDO;
import com.xinke.module.system.service.user.PostService;

@Tag(name = "管理后台 - 岗位信息")
@RestController
@RequestMapping("/system/post")
@Validated
public class PostController {

    @Resource
    private PostService postService;
    @Resource
    private DeptService deptService;
    @GetMapping(value = {"/list-all-simple", "simple-list"})
    @Operation(summary = "获取岗位全列表", description = "只包含被开启的岗位，主要用于前端的下拉选项")
    public Result<List<PostRespVO>> getSimplePostList() {
        // 获得岗位列表，只要开启状态的
        List<PostDO> list = postService.getPostList(null, Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
        // 排序后，返回给前端
        list.sort(Comparator.comparing(PostDO::getShowSort));
        return success(BeanUtils.toBean(list, PostRespVO.class));
    }

    @PostMapping("/create")
    @Operation(summary = "创建岗位信息")
    @PreAuthorize("@ss.hasPermission('system:post:create')")
    public Result<Long> createPost(@Valid @RequestBody PostSaveReqVO createReqVO) {
        return success(postService.createPost(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新岗位信息")
    @PreAuthorize("@ss.hasPermission('system:post:update')")
    public Result<Boolean> updatePost(@Valid @RequestBody PostSaveReqVO updateReqVO) {
        postService.updatePost(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete/{postId}")
    @Operation(summary = "删除岗位信息")
    @Parameter(name = "postId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:post:delete')")
    public Result<Boolean> deletePost(@PathVariable("postId") Long postId) {
        postService.deletePost(postId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得岗位信息")
    @Parameter(name = "postId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:post:query')")
    public Result<PostRespVO> getPost(@RequestParam("postId") Long postId) {
        PostDO post = postService.getPost(postId);
        return success(BeanUtils.toBean(post, PostRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得岗位信息分页")
    @PreAuthorize("@ss.hasPermission('system:post:query')")
    public Result<PageResult<PostRespVO>> getPostPage(@Valid PostPageReqVO pageReqVO) {
        PageResult<PostDO> pageResult = postService.getPostPage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }
        // 拼接数据
        Map<Long, DeptDO> deptMap = deptService.getDeptMap(
                convertList(pageResult.getList(), PostDO::getDeptId));

        return success(new PageResult<>(PostConvert.INSTANCE.convertList(pageResult.getList(), deptMap),
                pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出岗位信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:post:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPostExcel(@Valid PostPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PostDO> list = postService.getPostPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "岗位信息.xls", "数据", PostRespVO.class,
                        BeanUtils.toBean(list, PostRespVO.class));
    }

}